package com.cckat.base;

import com.cckat.utils.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 静海基地
 */

public abstract class BaseServiceImpl<T> {

    public abstract BaseMapper<T> getBaseMapper();

    public void insert(T role){
        getBaseMapper().insert(role);
    }

    public void delete(Long id){
        getBaseMapper().delete(id);
    }

    public void update(T role){
        getBaseMapper().update(role);
    }

    public T getById(Long id){
        return getBaseMapper().getById(id);
    }


    public PageInfo<T> findPage(Map<String, Object> filters){
        int pageNum = CastUtil.castInt(filters.get("pageNum"), 1);
        int pageSize = CastUtil.castInt(filters.get("pageSize"), 5);
        PageHelper.startPage(pageNum, pageSize);
        Page<T> page = getBaseMapper().findPage(filters);
        PageInfo<T> pageInfo = new PageInfo<>(page, 10);
        return pageInfo;
    }

    public List<T> findAll(){
        return getBaseMapper().findAll();
    }
}
